package com.emax.zhenghe.omsbase.modules.channel.cardbin;


import com.emax.channel.rpcapi.cardbin.CardBinService;
import com.emax.channel.rpcapi.cardbin.dto.CardBinDTO;
import com.emax.common.DubboApps;
import com.emax.zhenghe.common.api.vo.MyIPage;
import com.emax.zhenghe.common.api.vo.Result;
import com.emax.zhenghe.common.system.vo.LoginUser;
import com.emax.zhenghe.omsbase.common.aspect.annotation.AutoLog;
import com.emax.zhenghe.omsbase.modules.channel.cardbin.convert.CardBinVoConverter;
import com.emax.zhenghe.omsbase.modules.channel.cardbin.vo.CardBinVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@RestController
@RequestMapping("/cardBin")
public class CardBinController {

    @DubboReference(providedBy = DubboApps.CHANNEL_PROVIDER)
    private CardBinService cardBinService;

    @Autowired
    private CardBinVoConverter cardBinVoConverter;

    /**
     * 卡bin-分页查询
     */
    @AutoLog(value = "卡bin-分页查询")
    @ApiOperation(value = "卡bin-分页查询", notes = "卡bin-分页查询")
    @GetMapping("/page")
    public Result<MyIPage<CardBinVo>> page(CardBinVo vo,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("卡bin-分页查询>>>>>查询参数：{}", vo);
        CardBinDTO dto = cardBinVoConverter.fromVO(vo);
        MyIPage<CardBinDTO> safeBody = cardBinService.page(dto, pageNo, pageSize).getSafeBody();
        MyIPage<CardBinVo> cardBinVoMyIPage = safeBody.convertRecords(cardBinVoConverter::toVO);
        return Result.success(cardBinVoMyIPage);
    }

    /**
     *  卡bin-添加
     */
    @AutoLog(value = "卡bin-添加")
    @ApiOperation(value = "卡bin-添加", notes = "卡bin-添加")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody  CardBinVo vo) {
        log.info("卡bin-添加>>>>>查询参数：{}", vo);
        CardBinDTO dto = cardBinVoConverter.fromVO(vo);
        //获取登陆人
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取登陆人姓名
        dto.setCreateBy(sysUser.getRealname());
        dto.setUpdateBy(sysUser.getRealname());
        return cardBinService.save(dto);
    }

    /**
     *  卡bin-删除
     */
    @AutoLog(value = "卡bin-删除")
    @ApiOperation(value = "卡bin-删除", notes = "卡bin-删除")
    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestParam("cardBinId") Integer cardBinId) {
        log.info("卡bin-删除>>>>>查询参数：{}", cardBinId);
        return cardBinService.delete(cardBinId);
    }

    /**
     *  卡bin-更新
     */
    @AutoLog(value = "卡bin-更新")
    @ApiOperation(value = "卡bin-更新", notes = "卡bin-更新")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody CardBinVo vo) {
        log.info("卡bin-更新>>>>>查询参数：{}", vo);
        CardBinDTO dto = cardBinVoConverter.fromVO(vo);
        //获取登陆人
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取登陆人姓名
        dto.setUpdateBy(sysUser.getRealname());
        return cardBinService.update(dto);
    }

}
